package base.day04;

/**
 * 描述：
 *      转圈打印举证
 *
 * @author hl
 * @version 1.0
 * @date 2020/10/26 11:04
 */
public class Code_01_PrintMatrixSpiralOrder {
    public static void main(String[] args) {
//        int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},
//                {13, 14, 15, 16}};
//        spiralOrderPrint(matrix);
        //旋转逆序打印
        int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},
                {13, 14, 15, 16}};
        spiralOrderPrint(matrix);


    }

    public static void spiralOrderPrint(int[][] matrix) {
        int tR = 0;
        int tC = 0;
        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;
        boolean flag = true;
        while (tR < dR || tC < dC) {
            printEdge(matrix, tR++, tC++, dR--, dC--,flag);
            flag = !flag;
        }
    }

    //顺时针打印一周
    public static void printEdge(int[][] matrix, int tR, int tC, int dR, int dC) {

        if (tR == dR) {
            for (int i = tC; i <= dC; i++) {
                System.out.println(matrix[tR][i] + " ");
            }
        } else if (tC == dC) {
            for (int i = tR; i <= dR; i++) {
                System.out.println(matrix[i][tC] + " ");
            }
        } else {
            int curC = tC;
            int curR = tR;
            while (curC != dC) {
                System.out.println(matrix[tR][curC] + " ");
                curC++;
            }
            while (curR != dR) {
                System.out.println(matrix[curR][dC] + " ");
                curR++;
            }
            while (curC != tC) {
                System.out.println(matrix[dR][curC] + " ");
                curC--;
            }
            while (curR != tR) {
                System.out.println(matrix[curR][tC] + " ");
                curR--;
            }
        }
    }
    //根据参数决定顺/逆时针打印
    public static void printEdge(int[][] matrix, int tR, int tC, int dR, int dC, boolean isCW) {
        if (tR == dR) {
            for (int i = tC; i <= dC; i++) {
                System.out.println(matrix[tR][i] + " ");
            }
        } else if (tC == dC) {
            for (int i = tR; i <= dR; i++) {
                System.out.println(matrix[i][tC] + " ");
            }
        } else {
            int curC = tC;
            int curR = tR;
            if (isCW) {
                while (curC != dC) {//右
                    System.out.println(matrix[tR][curC] + " ");
                    curC++;
                }
                while (curR != dR) {//下
                    System.out.println(matrix[curR][dC] + " ");
                    curR++;
                }
                while (curC != tC) {//左
                    System.out.println(matrix[dR][curC] + " ");
                    curC--;
                }
                while (curR != tR) {//上
                    System.out.println(matrix[curR][tC] + " ");
                    curR--;
                }
            }else{
                while (curR != dR) {//下
                    System.out.println(matrix[curR][tC] + " ");
                    curR++;
                }
                while (curC != dC) {//右
                    System.out.println(matrix[dR][curC] + " ");
                    curC++;
                }
                while (curR != tR) {//上
                    System.out.println(matrix[curR][dC] + " ");
                    curR--;
                }
                while (curC != tC) {//左
                    System.out.println(matrix[tR][curC] + " ");
                    curC--;
                }
            }

        }
    }

}
